package com.lx.nba.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CountDownLatch;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;

import com.google.gson.Gson;
import com.lx.nba.dao.ManDao;
import com.lx.nba.dao.TeamDao;
import com.lx.nba.entiy.Man;
import com.lx.nba.entiy.Team;
import com.lx.nba.http.HttpGet;
import com.lx.nba.parse.ContentParse;
import com.lx.nba.parse.impl.ManParseImpl;
import com.lx.nba.service.DownLoadDataService;
import com.lx.nba.service.InitNBAData;
import com.lx.nba.util.Content;

public class InitNBAManImpl implements InitNBAData, DownLoadServiceCallback{

	@Autowired
	private DownLoadDataService service ;
	
	@Autowired
	private TeamDao teamDao ;
	
	@Autowired
	private ManDao manDao ;
	
	private Map<Integer, String> teamMap ;
	
	private CountDownLatch latch ;
	
	@Override
	public void init() {
		// TODO Auto-generated method stub
		
		List<Team> teams = teamDao.getAllTeams() ;
		
		if( teams.isEmpty() ){
			
			return ;
			
		}else{
			
			teamMap = new HashMap<>() ;
			
			for( Team team : teams ){

				teamMap.put(team.getTeamId(), team.getName()) ;
				
			}
			
		}
		
		List<Content> contents = manListContent() ;
		
		latch = new CountDownLatch(contents.size()) ;
		
		for( Content content:contents){
			
			service.download(content, this);
			
		}
		
		try {
			
			System.out.println("NBA Man is initing");
			
			latch.await();
			
			System.out.println("NBA Man finish initing");
			
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
			System.out.println("init error");
			
		} 
		
	}
		
	private List<Content> manListContent(){
		
		List<Content> contents = new ArrayList<>() ;
		
		for( int i = 0 ; i < 30 ; i++){
			
			StringBuffer params = new StringBuffer() ;
			
			for( int j = 0 ; j < 100 ; j++){
				
				params.append("%2C" +  ( i * 100 + j + 3000 ) ) ;
				
			}
			Content content = new Content() ;
			content.setUrl("http://ziliaoku.sports.qq.com/cube/index?cubeId=8&dimId=5&params=t1%3A"+params.toString()+"&from=sportsdatabase");
			contents.add(content) ;
			
		}
		
		return contents ;
		
	}

	@Override
	public void callback(Content content) {
		// TODO Auto-generated method stub
		
		String text = content.getContent() ;
		
		text = text.substring(text.indexOf('{')+1, text.lastIndexOf('}')) ;
		text = text.substring(text.indexOf('{'),text.lastIndexOf('}')+1) ;
		
		
		Gson gson = new Gson() ;
		
		Base base = gson.fromJson(text, Base.class) ;
		
		if( base.getPlayerBaseInfo() == null ){
			
			latch.countDown();
			
			return ;
			
		}
		
		for( ManDetail detail : base.getPlayerBaseInfo()){
			
			if( detail.getPicFromSIB().equals("http://mat1.gtimg.com/sports/nba/logo/qymoren.png")){
				
				continue ;
				
			}
			
			Man man = new Man() ;
			man.setName(detail.getEnName());
			man.setIcon(detail.getPicFromSIB());
			man.setNum(detail.getJerseyNum());
			man.setPosition(detail.getPosition());
			man.setHeight(detail.getHeight());
			man.setWeight(detail.getWeight());
			man.setPlayerId(detail.getPlayerId());
			man.setTeam(teamMap.get(detail.getTeamId()));
			
			manDao.saveMan(man);
			
		}
		
		latch.countDown(); 
		
	}
	
	
	class Base{
		
		private List<ManDetail> playerBaseInfo ;

		public List<ManDetail> getPlayerBaseInfo() {
			return playerBaseInfo;
		}

		public void setPlayerBaseInfo(List<ManDetail> playerBaseInfo) {
			this.playerBaseInfo = playerBaseInfo;
		}
		
		
	}

	
	class ManDetail{
		
		private String enName ;
		
		private String picFromSIB ;
		
		private int teamId ;
		
		private int playerId ;
		
		private int jerseyNum ;
		
		private String position ;
		
		private String weight ;
		
		private String height ;

		public String getEnName() {
			return enName;
		}

		public void setEnName(String enName) {
			this.enName = enName;
		}

		public String getPicFromSIB() {
			return picFromSIB;
		}

		public void setPicFromSIB(String picFromSIB) {
			this.picFromSIB = picFromSIB;
		}

		public int getTeamId() {
			return teamId;
		}

		public void setTeamId(int teamId) {
			this.teamId = teamId;
		}

		public int getJerseyNum() {
			return jerseyNum;
		}

		public void setJerseyNum(int jerseyNum) {
			this.jerseyNum = jerseyNum;
		}

		public String getPosition() {
			return position;
		}

		public void setPosition(String position) {
			this.position = position;
		}

		public String getWeight() {
			return weight;
		}

		public void setWeight(String weight) {
			this.weight = weight;
		}

		public String getHeight() {
			return height;
		}

		public void setHeight(String height) {
			this.height = height;
		}

		public int getPlayerId() {
			return playerId;
		}

		public void setPlayerId(int playerId) {
			this.playerId = playerId;
		}
		
		
		
	}
	
	
	
}
